//===================================================================================
// Microsoft patterns & practices
// Silk : Web Client Guidance
//===================================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===================================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using DataTier.DataAccess.UnitOfWork;
using DataTier.DataCore;
using DataTier.DataCore.Repositories;
using DataTier.DataCore.UnitOfWork;
using DataTier.Model;
using DataTier.Model.Entities;

namespace DataTier.DataAccess.Repositories
{
    public class ReminderRepository : Repository<Reminder, int>, IReminderRepository
    {
        public ReminderRepository(IQueryableUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }

        public void Create(int vehicleId, Reminder reminder)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {
                reminder.VehicleId = vehicleId;
                context.CreateSet<Reminder>().Add(reminder);
            }
            this.UnitOfWork.Commit();
        }

        public Reminder GetReminder(int reminderId)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {
                return context.CreateSet<Reminder>().Single(v => v.ReminderId == reminderId);
            }
            return null;
        }

        public void Update(Reminder updatedReminder)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {

                context.CreateSet<Reminder>().Attach(updatedReminder);
                context.SetEntityState(updatedReminder, updatedReminder.VehicleId == 0
                                                            ? EntityState.Added
                                                            : EntityState.Modified);
                this.UnitOfWork.Commit();
            }

        }

        public void Delete(int reminderId)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {
                Reminder reminderToDelete = this.GetReminder(reminderId);
                context.CreateSet<Reminder>().Remove(reminderToDelete);
                this.UnitOfWork.Commit();
            }
        }

        public IEnumerable<Reminder> GetOverdueReminders(int vehicleId, DateTime forDate, int odometer)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {
                return context.CreateSet<Reminder>().Where(r => (r.VehicleId == vehicleId) && (r.DueDate < forDate || r.DueDistance < odometer) && !r.IsFulfilled);
            }
            return null;
        }

        public IEnumerable<Reminder> GetUpcomingReminders(int vehicleId, DateTime forStartDate, DateTime forEndDate,
                                                          int odometer, int warningOdometer)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {
                return context.CreateSet<Reminder>().Where(r => (r.VehicleId == vehicleId) && !r.IsFulfilled &&
                         ((r.DueDate > forStartDate && r.DueDate < forEndDate) ||
                          (r.DueDistance > odometer && r.DueDistance <= odometer + warningOdometer)));
            }
            return null;
        }

        public IEnumerable<Reminder> GetRemindersForVehicle(int vehicleId)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {
                return context.CreateSet<Reminder>().Where(v => v.VehicleId == vehicleId);
            }
            return null;
        }

        public override Reminder Get(int id)
        {
            var context = this.UnitOfWork as MainModuleUnitOfWork;
            if (context != null)
            {
                return context.CreateSet<Reminder>().Find(id);
            }
            return null;
        }
    }
}
